package com.mflex.WHSmartWhouseAd.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mflex.WHSmartWhouseAd.R;
import com.mflex.WHSmartWhouseAd.adapter.CommonDecoration;
import com.mflex.WHSmartWhouseAd.adapter.MenuAdapter;
import com.mflex.WHSmartWhouseAd.adapter.RecyclerViewAdapter;
import com.mflex.WHSmartWhouseAd.listener.OnRecyclerItemClickListener;
import com.mflex.WHSmartWhouseAd.listener.OnRecyclerViewItemClickListener;
import com.mflex.WHSmartWhouseAd.model.AuthorityModel;
import com.mflex.WHSmartWhouseAd.model.MenuBean;
import com.mflex.WHSmartWhouseAd.model.MenuModel;

import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.net.JsonUtil;
import com.xuexiang.xhttp2.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

//import butterknife.ButterKnife;

public class MainActivity extends BaseActivity {
    private List<MenuModel> menuModelList = new ArrayList<>();
    private List<MenuModel> menuModelList2 = new ArrayList<>();
    private List<MenuModel> menuModelList3 = new ArrayList<>();

    RecyclerView recyclerView;
    RecyclerView recyclerView2;
    RecyclerView recyclerView3;

    private RecyclerViewAdapter adapter;

    private String EmpNo;
    private String userName;
    private String userPassword;

    MiniLoadingDialog mMiniLoadingDialog;
    private List<MenuBean> listDatas = new ArrayList<MenuBean>();
    private List<AuthorityModel> auList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     //   ButterKnife.bind(this);

//        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "加载...");
        Intent intent = getIntent();

        userName=intent.getStringExtra("userName");

        userPassword = intent.getStringExtra("userPassword");

        Type listType = new TypeToken<ArrayList<AuthorityModel>>() {
        }.getType();
        auList = (List<AuthorityModel>) JsonUtil.fromJson(intent.getStringExtra("auth"), listType);
//        try {
//            init();
//        } catch (ClassNotFoundException e) {
//            throw new RuntimeException(e);
//        }

        recyclerView=findViewById(R.id.recycler_view);
        recyclerView2=findViewById(R.id.recycler_view2);
        recyclerView3=findViewById(R.id.recycler_view3);

        try {
         for (AuthorityModel s : auList) {
             //库位管理
             if(StringUtils.equals("库位管理",s.getGroupName().toString()))
             {
                 menuModelList.add(new MenuModel(s.getFunctionName().toString(), android.R.drawable.ic_menu_save));
             }
             //出库管理
             if(StringUtils.equals("出库管理",s.getGroupName().toString()))
             {
                 menuModelList2.add(new MenuModel(s.getFunctionName().toString(), android.R.drawable.ic_menu_save));
             }
             //其他
             if(StringUtils.equals("其他",s.getGroupName().toString()))
             {
                 menuModelList3.add(new MenuModel(s.getFunctionName().toString(), android.R.drawable.ic_menu_save));
             }
         }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
//        MenuModel ic_menu_pkg_voc = new MenuModel("出库指令", android.R.drawable.ic_menu_save);
//        menuModelList.add(ic_menu_pkg_voc);
//        MenuModel ic_menu_pkg_voc1 = new MenuModel("盘上载具", android.R.drawable.ic_menu_save);
//        menuModelList.add(ic_menu_pkg_voc1);
//        MenuModel ic_menu_sampling_task = new MenuModel("原材料抽检", android.R.drawable.ic_menu_save);
//        menuModelList.add(ic_menu_sampling_task);
//        MenuModel ic_menu_returnsampling_task = new MenuModel("抽检归还", android.R.drawable.ic_menu_save);
//        menuModelList.add(ic_menu_returnsampling_task);
//
//        //出库管理
//        MenuModel ic_menu_Shipment_Sap = new MenuModel("柔板投料下架",android.R.drawable.ic_menu_save);
//        menuModelList2.add(ic_menu_Shipment_Sap);
//        MenuModel ic_menu_Material_Sap = new MenuModel("SMT投料出库", android.R.drawable.ic_menu_save);
//        menuModelList2.add(ic_menu_Material_Sap);
//        MenuModel ic_menu_SMTMaterial_Sap = new MenuModel("原材料出库",android.R.drawable.ic_menu_save);
//        menuModelList2.add(ic_menu_SMTMaterial_Sap);
////        MenuModel ic_menu_TaskBOX_Sap = new MenuModel("整箱任务出库", android.R.drawable.ic_menu_save);R.drawable.icon_more_operation_share_chat
////        menuModelList2.add(ic_menu_TaskBOX_Sap);
//        MenuModel ic_menu_ShelfOffOutStock_Sap = new MenuModel("仓库下架出库", android.R.drawable.ic_menu_save);
//        menuModelList2.add(ic_menu_ShelfOffOutStock_Sap);
//        MenuModel ic_menu_qms_task_query = new MenuModel("抽检出库", android.R.drawable.ic_menu_save);
//        menuModelList2.add(ic_menu_qms_task_query);
//
//
//
//        //其他
//        MenuModel ic_menu_pkg_voc3 = new MenuModel("出货二次确认", android.R.drawable.ic_menu_save);
//        menuModelList3.add(ic_menu_pkg_voc3);
//        MenuModel ic_menu_combined_vehicle = new MenuModel("合并载具", android.R.drawable.ic_menu_save);
//        menuModelList3.add(ic_menu_combined_vehicle);
//
//        MenuModel ic_menu_line_receive = new MenuModel("产线退回接收", android.R.drawable.ic_menu_save);
//        menuModelList3.add(ic_menu_line_receive);
//        MenuModel ic_menu_line_back = new MenuModel("产线退回发送", android.R.drawable.ic_menu_save);
//        menuModelList3.add(ic_menu_line_back);
//
//        MenuModel ic_menu_car_receive = new MenuModel("车接收", android.R.drawable.ic_menu_save);
//        menuModelList3.add(ic_menu_car_receive);
//        MenuModel ic_menu_car_back = new MenuModel("车发送", android.R.drawable.ic_menu_save);
//        menuModelList3.add(ic_menu_car_back);
//
//        MenuModel ic_menu_stock_task = new MenuModel("盘点", android.R.drawable.ic_menu_save);
//        menuModelList3.add(ic_menu_stock_task);


        //设置布局方式，可以用LinearLayout，RelativeLayout等，这里用GridLayoutManager，因为可以方便得配置一行显示多少个item，这里显示3个
        GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView2.setLayoutManager( new GridLayoutManager(this, 3));
        recyclerView3.setLayoutManager( new GridLayoutManager(this, 3));

        MenuAdapter adapter = new MenuAdapter(menuModelList);
        recyclerView.setAdapter(adapter);

        MenuAdapter adapter2 = new MenuAdapter(menuModelList2);
        recyclerView2.setAdapter(adapter2);

        MenuAdapter adapter3 = new MenuAdapter(menuModelList3);
        recyclerView3.setAdapter(adapter3);

        //设置点击，这个需要自己写，官方没有直接提供方法

        //库位管理
        adapter.setOnRecyclerViewItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent;
                switch (menuModelList.get(position).getName()) {
                    case "出库指令":
                        intent = new Intent(MainActivity.this, OutShelfActivity.class);
                        startActivity(intent);
                        break;
                    case "盘上载具":
                        intent = new Intent(MainActivity.this, DSPlateInBoxActivity.class);
                        intent.putExtra("userName",userName);
                        startActivity(intent);
                        break;
                    case "原材料抽检":
                        intent = new Intent(MainActivity.this, QMSsampLingTaskActivity.class);
                        startActivity(intent);
                        break;
                    case "抽检归还":
                        intent = new Intent(MainActivity.this, QMSSamplingReturnActivity.class);
                        startActivity(intent);
                        break;
                    default:
                        break;
                }
            }
        });

        //出库管理
        adapter2.setOnRecyclerViewItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent;
                switch (menuModelList2.get(position).getName()) {
                    case "智能仓箱出库":
                        intent = new Intent(MainActivity.this, BoxOutStockActivity.class);
                        intent.putExtra("userName",userName);
                        intent.putExtra("userPassword",userPassword);
                        startActivity(intent);
                        break;
                    case "柔板投料下架":
                        intent = new Intent(MainActivity.this, FlexShelfOffNewActivity.class);
                        intent.putExtra("userName",userName);
                        intent.putExtra("userPassword",userPassword);
                        startActivity(intent);
                        break;
                    case "SMT投料出库":
                        intent = new Intent(MainActivity.this, SMTMaterialOutStockActivity.class);
                        intent.putExtra("userName",userName);
                        intent.putExtra("userPassword",userPassword);
                        startActivity(intent);
                        break;
                    case "原材料出库":
                        intent = new Intent(MainActivity.this, MaterialOutStockActivity.class);
                        intent.putExtra("userName",userName);
                        intent.putExtra("userPassword",userPassword);
                        startActivity(intent);
                        break;
                    case "抽检出库":
                        intent = new Intent(MainActivity.this, QMSTaskQueryActivity.class);
                        startActivity(intent);
                        break;
                    case "仓库下架出库":
                        intent = new Intent(MainActivity.this, WMSOutStockActivity.class);
                        startActivity(intent);
                        break;

                    default:
                        break;
                }
            }
        });
        //其他
        adapter3.setOnRecyclerViewItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent;
                switch (menuModelList3.get(position).getName()) {
                    case "车接收":
                        intent = new Intent(MainActivity.this, PDACarReceivingActivity.class);
                        startActivity(intent);
                        break;
                    case "车发送":
                        intent = new Intent(MainActivity.this,PDACarSendingActivity.class);
                        startActivity(intent);
                        break;
                    case "产线退回接收":
                        intent = new Intent(MainActivity.this, ProductionLineBackReceiveActivity.class);
                        startActivity(intent);
                        break;
                    case "产线退回发送":
                        intent = new Intent(MainActivity.this,ProductionLineSentBackActivity.class);
                        startActivity(intent);
                        break;
                    case "出货二次确认":
                        intent = new Intent(MainActivity.this, ShippingInquiryActivity.class);
                        startActivity(intent);
                        break;
                    case "合并载具":
                        intent = new Intent(MainActivity.this, CombinedVehicleActivity.class);
                        startActivity(intent);
                        break;
                    case "盘点":
                        intent = new Intent(MainActivity.this, StockTaskListActivity.class);
                        startActivity(intent);
                        break;

                    default:
                        break;
                }
            }
        });
    }
//    private void init() throws ClassNotFoundException {
//        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
//        loadMenuData();
//
//        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
//        recyclerView.addItemDecoration(new CommonDecoration(this));
//
//        adapter = new RecyclerViewAdapter(this, listDatas);
//
//        recyclerView.setAdapter(adapter);
//
//        adapter.setOnItemClickListener(new OnRecyclerItemClickListener() {
//            @Override
//            public void onRecyclerItemClick(RecyclerView parent, View view, int position) {
//                Intent intent = new Intent(MainActivity.this, listDatas.get(position).getTargertActivty());
//                intent.putExtra("userName", userName);
//                intent.putExtra("title", listDatas.get(position).getTxt_info());
//                startActivity(intent);
//            }
//        });
//
//    }
//    private void loadMenuData() throws ClassNotFoundException {
//
//        for (AuthorityModel s : auList) {
//            MenuBean menuBean =  new MenuBean(s.getFunctionName(),"",Integer.parseInt(s.getICON()),Class.forName(s.getActivity()));
//            listDatas.add(menuBean);
//
//        }
//    }
    @Override
    public void onClick(View view) {

    }

    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        return false;
    }
}